<?php
/**
 * 添加js文件
 * @param unknown $dirname
 * @param unknown $file_name
 */
function js($dirname, $file_name, $version=1.0)
{
    echo '<script type="text/javascript" src="'.$dirname.'/js/'.$file_name.'.js?v='.$version.'"></script>';
}

/**
 * 添加css文件
 * @param unknown $dirname
 * @param unknown $file_name
 */
function css($dirname, $file_name, $version=1.0)
{
    echo '<link rel="stylesheet" type="text/css" href="'.$dirname.'/css/'.$file_name.'.css?v='.$version.'"/>';
}

/**
 * 返回html提示信息
 * @return string
 */
function execute_alert_message()
{
    $CI = & get_instance();
    $CI->load->library('session');
    if ($CI->session->flashdata('success')) {
        return '<div class="alert alert-success" role="alert">
                    <span class="glyphicon glyphicon glyphicon-ok" aria-hidden="true"></span>
                    <span class="sr-only">Success:</span>
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>'.$CI->session->flashdata('success').'
               </div>';
    } elseif ($CI->session->flashdata('error')) {
        return '<div class="alert alert-danger" role="alert">
                    <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                    <span class="sr-only">Error:</span>
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>'.$CI->session->flashdata('error').'
               </div>';
    }
}

function info_alert_message($info)
{
    return '<div class="alert-info">
                <button class="close" data-dismiss="alert"></button>
                <strong>Warning!</strong> ' . $info . '
            </div>';
}

/**
 * 可视化打印。
 * @param mixed $data
 */
function pr($data)
{
    echo '<pre>';
    print_r($data);
    echo '</pre>';
}

/**
 * 可视化打印。
 * @param mixed $data
 */
function dump($data)
{
    echo '<pre>';
    var_dump($data);
    echo '</pre>';
}

function redirectAction($uri = '', $method = 'location', $http_response_code = 302)
{
    if (!preg_match('#^https?://#i', $uri)) {
        $uri = base_url($uri);
    }

    switch($method) {
        case 'refresh' : 
            header("Refresh:0;url=".$uri);
            break;
        default : 
            header("Location: ".$uri, TRUE, $http_response_code);
            break;
    }
    exit;
}

/**
 * Validate mobile phone
 * @param unknown $mobile
 * @return boolean
 */
function valid_mobile($mobile)
{
    return (!preg_match('/^1[34578]\d{9}$/', $mobile)) ? FALSE : TRUE;
}
